<html>
<head>
<link rel='stylesheet' href='../../../js/sh/SyntaxHighlighter.css' type='text/css' />
<script src='../../../js/sh/shCore.js'></script>
<script src='../../../js/sh/shBrushJava.js'></script>
<style>
* {
font-family:Courier New,monospace;
  padding: 0;
  margin: 0;
  white-space: nowrap;
  font-size: 11px;
}
.dp-highlighter {
  white-space: nowrap;
  overflow: visible;
  width: 600px;
  font-size: 11px;
  font-family:Courier New,monospace;
}
</style>
</head>
<body>
<textarea name='code' class='java:nogutter' rows='15' cols='120'>
/*
 * SmartGWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * SmartGWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  SmartGWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;

import java.util.HashMap;
import java.util.Map;

public class FormDependentSelectsSample implements EntryPoint {

    public void onModuleLoad() {

        final DynamicForm form = new DynamicForm();
        form.setWidth(500);
        form.setNumCols(4);

        final Map<String, String[]> departments = new HashMap<String, String[]>();
        departments.put("Marketing", new String[]{"Advertising", "Community Relations"});
        departments.put("Sales", new String[]{"Channel Sales", "Direct Sales"});
        departments.put("Manufacturing", new String[]{"Design", "Development", "QA"});
        departments.put("Services", new String[]{"Support", "Consulting"});

        SelectItem divisionItem = new SelectItem();
        divisionItem.setName("division");
        divisionItem.setTitle("Division");
        divisionItem.setValueMap("Marketing", "Sales", "Manufacturing", "Services");
        divisionItem.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                String selectedItem = (String) event.getValue();
                form.getField("department").setValueMap(departments.get(selectedItem));
            }
        });

        SelectItem departmentItem = new SelectItem();
        departmentItem.setName("department");
        departmentItem.setTitle("Department");
        departmentItem.setAddUnknownValues(false);

        form.setItems(divisionItem, departmentItem);

        form.draw();
    }

}
</textarea>
<script class='javascript'>
dp.SyntaxHighlighter.HighlightAll("code");
</script>
</body>
</html>
